/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package stage;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.Coordinate;
import java.util.Vector;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;

/**
 *
 * @author matteo
 */
public class MainProva {
    public static void main(String args[]){

//        double r=2.0;
//        double c=2.0;
//        double width=5.0;
//
//
//        Coordinate coord[]=new Coordinate[]{new Coordinate(c*width,r*width),new Coordinate(c*width+width,r*width),new Coordinate(c*width+width,r*width+width),new Coordinate(c*width,r*width+width),new Coordinate(c*width,r*width)};
//        Geometry g1=new GeometryFactory().createLinearRing(coord);
//        Geometry quad=new GeometryFactory().createPolygon((LinearRing)g1, null);
//
//        System.out.println("quad: "+quad.toText());

//        Coordinate coord[]=new Coordinate[]{new Coordinate(35,35),new Coordinate(35,50),new Coordinate(50,50),new Coordinate(50,35),new Coordinate(35,35)};
//        Geometry g1=new GeometryFactory().createLinearRing(coord);
//        Geometry quad=new GeometryFactory().createPolygon((LinearRing)g1, null);
//
//        System.out.println("quad: "+quad.toText());
//
//        Coordinate coord2[]=new Coordinate[]{new Coordinate(40.0,40.0),new Coordinate(30.0,40.0)};
//        Geometry linea=new GeometryFactory().createLineString(coord2);
//        Geometry intersezione=quad.intersection(linea);
//        System.out.println("linea: "+linea.toText());
//        System.out.println("intersezione: "+intersezione.toText());
//
//        System.out.println("quad: "+intersezione.toText());
//        SpatialGrid sg=new SpatialGrid(15.0,45.0,15.0,45.0,10.0);
        //sg.getDimension();

//        System.out.println("X_i: "+sg.getX_i()+"X_f: "+sg.getX_f()+"Y_i: "+sg.getY_i()+"Y_f: "+sg.getY_f());

        //System.out.println("righe di sg: "+(int)(sg.getRow()-1));

//        Coordinate coord2[]=new Coordinate[]{new Coordinate(2.0,4.0),new Coordinate(5.0,5.0)};
//        Geometry linea=new GeometryFactory().createLineString(coord2);
//
//        Coordinate coord3[]=new Coordinate[]{new Coordinate(1.0,4.0),new Coordinate(1.0,5.0)};
//        Geometry linea3=new GeometryFactory().createLineString(coord3);

//
//        GridUtils.inserisci(linea, sg, 1);
//        GridUtils.inserisci(linea3, sg, 2);

        String localdir = System.getProperty("user.home");
        String path1 = localdir+"/Dropbox/stage/prova_poligoni_tabe_fiumi.jml";
        String path2 = localdir+"/Dropbox/stage/prova_poligoni_tabe6.jml";

        ElaboratoreFile ef=new ElaboratoreFile(path1, "IDRO_");
        ElaboratoreFile ef1=new ElaboratoreFile(path2, "STRADE_");
        GeometryContainer gc=ef.getNextGeometry();


//        Vector v2=ElaborazioneFile.geomArray(path2, "STRADE_ID");
//        Vector v1=ElaborazioneFile.geomArray(path1, "STRADE_ID");
//
////        Geometry app = gc.getGeometryN(0);
////        System.out.println("(main)Geometry file1 0: "+app.getEnvelope().toText());
////        app = gc.getGeometryN(1);
////        System.out.println("(main)Geometry file1 1: "+app.getEnvelope().toText());
////
////        app = mbr.getGeometryN(0);
////        System.out.println("(main)Geometry file1 0: "+app.getEnvelope().toText());
//
        double xi = 500000.0,xf = 800000.0,yi =5000000.0, yf = 5300000.0, w = 100000.0 ;

//        System.out.println("(main)dimensioni: \n"+xi+"\n"+xf+"\n"+yi+"\n"+yf+"\n");

        SpatialGrid sg1=new SpatialGrid(xi,xf,yi,yf,w);
        GridUtils.createGridJml(sg1);
        SpatialGrid sg2=new SpatialGrid(xi,xf,yi,yf,w);

//        System.out.println("Colonne: "+sg1.getColumn()+", righe: "+sg1.getRow());
////
////        sg1.getDimension();
//        GeometryContainer gc;
        Vector v;
        GeometryContainer gc1=  new GeometryContainer();
         gc1 = ef1.getNextGeometry();
        int cont = 0;

        // gc.getGeometry()!=null ||
        while(gc.getGeometry()!=null || gc1.getGeometry() != null){
//            System.out.println("CONT : "+cont);
            
            if( gc.getGeometry() != null ){
                v = GridUtils.join(sg2, gc);
                sg1.inserisci(gc);
//
//
//            System.out.println("IDRO id : "+gc.getId()+" N_INTERSEZIONI: "+v.size());
                if (v.size() > 0) {
                    GridUtils.createJMLResult(v, "risultati1", "ID");
                }
            }
            gc = ef.getNextGeometry();
////            System.out.println("Stampa gc");
////            gc.stampaGeometryContainer();

            v = new Vector();

            
//            System.out.println("Gc1 id : "+gc1.getId());
//            System.out.println("Stampa gc2");
//            gc1.stampaGeometryContainer();
            if( gc1.getGeometry() != null ){
                v = GridUtils.join(sg1, gc1);
                sg2.inserisci(gc1);

//                System.out.println("STRADE id : "+gc1.getId()+" N_INTERSEZIONI: "+v.size());
                if (v.size() > 0) {
                    GridUtils.createJMLResult(v, "risultati1", "ID");
                }
            }
            gc1 = ef1.getNextGeometry();
            cont ++;
        }
                //System.out.println("Cont = "+cont);
        System.out.println("Stampa grid 1");
        sg1.stampaGrid();

        sg1.stampaResDist();

        System.out.println("Stampa grid 2");
        sg2.stampaGrid();



        sg2.stampaResDist();


//
//        sg1.insert(3, 1, null);
//
//        System.out.println("(main) -------------------------------------------------------------");
//        System.out.println("(main) inserimento null");
//
//        ListaLinked ll = new ListaLinked();
//        ListaLinked ll1 = sg1.getLL(2, 2);
//
//        sg1.insertLL(2, 2, ll);
//        sg1.stampaGrid();
//
//        sg1.insertLL(2, 2, ll1);
//        sg1.stampaGrid();

//
//        System.out.println("(main) inserimento nella sg2");
//        for (int i = 0; i < v2.size(); i ++){
//            gc = (GeometryContainer)v1.elementAt(i);
//
//            v = GridUtils.join(sg2, gc);
//            sg1.inserisci(gc);
//
//            if ( v.size() > 0 ){
//                GridUtils.createJMLResult(v, "risultati1", "STRADE_ID");
//            }
//        }

//        sg2.stampaGrid();
//
//        Geometry app = gc.getGeometryN(0);
////        System.out.println("Geometry che controllo: "+app.toText());
//        GeometryContainer gc1 = (GeometryContainer)gc.elementAt(0);
//        v = GridUtils.join(sg2, gc1);
//        sg2.stampaGrid();
//
////        System.out.println("Dimensione v: "+v.size());
//        if( v.size() > 0){
//            GeometryIntersection gi = (GeometryIntersection)v.elementAt(0);
//            System.out.println("Dimensione v: "+v.size());
//            System.out.println("Intersezione tra elemento: "+gi.getId()+" e l'elemento: "+gi.getIntersection_id());
//        }
//
//        GridUtils.createJMLResult(v,"STRADE_ID");
//
//        gc1 = (GeometryContainer)gc.elementAt(1);
//        v = GridUtils.join(sg2, gc1);
//
//        GridUtils.createJMLResult(v,"STRADE_ID");
            

//        sg.getDimension();
//
//        Geometry m = (Geometry)mbr.getGeometryN(0);
//        System.out.println("(main)Mbr: "+m.toText());
//
//        Vector v = GridUtils.getMbrElement(sg, m);
//
//        GeometryContainer gco = (GeometryContainer)v.elementAt(0);
//        Geometry g = gco.getGeometry();
//        System.out.println("Elemento trovato da mbr: "+g.toText());

        //sg.toText(2, 0);
        




    }
}
